package com.ymdx.test;

import com.alibaba.fastjson.JSONObject;
import com.ymdx.entity.News;
import com.ymdx.entity.User;
import com.ymdx.service.NewsService;
import com.ymdx.service.UserService;
import com.ymdx.util.ExcelUtil;
import com.ymdx.util.Page;
import com.ymdx.util.tool.BeanCopyUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.beans.PropertyDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by yangliwei on 2017/8/8.
 */
public class Demo1 {
    @Test
    public void test01() {
        ApplicationContext ac = new FileSystemXmlApplicationContext("D:\\ideaProject\\ymdx\\src\\main\\webapp\\WEB-INF\\applicationContext.xml");
//        PersonService personService = (PersonService) ac.getBean("personService");
//        personService.save(new Person());
        UserService userService = (UserService) ac.getBean("userService");
        System.out.println(userService.getUserByMobile("21"));
        userService.save(new User());
//        System.out.println(userService.getById(User.class,1L));
//        BaseDao userDao = (BaseDao) ac.getBean("userDao");
//        User user = new User();
//        user.setMobile(new Integer((int )Math.random()*1000000).toString());
//        userDao.save(user);

        News news = new News();
        news.setAuthor("暗室");
        NewsService newsService = (NewsService) ac.getBean("newsService");
        List list = newsService.getByExample(news);
        System.out.println(list.size());


    }


    /**
     * 测试加密
     */
    @Test
    public void test02() {
        System.out.println(DigestUtils.md5Hex("123456"));

    }

    /**
     * 测试 属性赋值
     */
    @Test
    public void test03() {
        User user1 = new User();
        user1.setNickName("ylw");
        user1.setEmail("");
        User user2 = new User();
        user2.setEmail("yyyy");
        BeanCopyUtil.copyProperties(user1, user2, true);
        System.out.println(user1);
        System.out.println(user2);
    }

    /**
     * 测试fastJson
     */
    @Test
    public void test04() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa", "bbb");
        jsonObject.put("int", 1);
        System.out.println(jsonObject);
        System.out.println(jsonObject.toJSONString());
    }

    /**
     * 生成admin账号密码 f6fdffe48c908deb0f4c3bd36c032e72
     */
    @Test
    public void test05() {
        System.out.println(DigestUtils.md5Hex("admin" + "admin"));
    }


    //    使用反射读取类信息
    @Test
    public void test06() {
        News news = new News();
        news.setTitle("title");
        news.setIntroduction("introduction");
        news.setCreateTime(new Date());
        news.setAuthor("author");
        String[] params = {"title", "introduction", "author", "createTime"};
        Object obj = news;
        for (String param : params) {
            try {
                Method getMethod = obj.getClass().getMethod("get" +firstToUppercase(param));
                System.out.println(getMethod);
                System.out.println(getMethod.invoke(obj));
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    public static String firstToUppercase(String src) {
        StringBuilder sb = new StringBuilder(src);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        return sb.toString();
    }

    /**
     * 测试Excel类的输出
     */
    @Test
    public void test07() throws IOException {
        Workbook wb = new HSSFWorkbook();
        Sheet[] sheet = new Sheet[1];
//        Sheet sheet = wb.createSheet();
        Integer rowNum = 0;
        Page<News> page  = new Page<>();
        String[] header = {"11111111111111111","2222222222222222","333333333333333","44444444444444444"};
        String[] params = {"title", "introduction", "author", "createTime"};

        List<News> list = new ArrayList<>();
        for(int i = 0 ;i<10;i++){
            News news = new News();
            news.setTitle("title"+i);
            news.setIntroduction("introduction"+i);
            news.setCreateTime(new Date());
            news.setAuthor("author"+i);
            list.add(news);
        }
        page.setTotalCount(10);
        page.setResult(list);
        ExcelUtil.write2workbook(wb,sheet,page,rowNum,params,header);
        FileOutputStream fileOut = new FileOutputStream("D:\\ideaProject\\functionUtil\\src\\main\\tempfile\\workbook.xls");
        wb.write(fileOut);
        fileOut.close();
    }


    @Test
    public void test(){
        List<Integer>list = Arrays.asList(10,20,30,null);
        System.out.println(list.size());
        list.subList(2,4).set(0,40);
//        list = new ArrayList<Integer>(list);
        list.set(3,100);
    }

    /**
     * 将空的字符串设置成""
     */
    @Test
    public void test08() throws IllegalAccessException, InvocationTargetException {
//        需要设置私有变量权限
//        User user = new User();
//        Field[]  fields= user.getClass().getDeclaredFields();
//        for(Field field:fields){
//            System.out.println(field.getType().getName().equalsIgnoreCase("java.lang.String"));
//            if(field.getType().getName().equalsIgnoreCase("java.lang.String")&&(field.get(user)==null||field.get(user).toString().length()==0)){
//                field.set(user,"");
//            }
//        }


//方法2
        User uu = new User();
        BeanWrapper src = new BeanWrapperImpl(uu);
        PropertyDescriptor[] propertyDescriptors = src.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            String propName = propertyDescriptor.getName();
            if (propertyDescriptor.getReadMethod() != null ) {
                System.out.println(propName+"    "+propertyDescriptor.getReadMethod().getReturnType().getName());
                if (propertyDescriptor.getReadMethod().getReturnType().getName().equalsIgnoreCase("java.lang.String")&&src.getPropertyValue(propName)==null) {
                    propertyDescriptor.getWriteMethod().invoke(uu,"");
                }
            }
        }
        System.out.println(uu);
    }

}
